package edu.contoller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.annotations.LoginRequired;
import edu.entity.Do.Product;
import edu.entity.Do.ProductImage;
import edu.entity.Dto.AddProductDto;
import edu.entity.Vo.ProductVo;
import edu.mapper.ProductImageMapper;
import edu.mapper.ProductMapper;
import edu.utils.JwtUtil;
import org.apache.catalina.core.ApplicationPart;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/product")
@CrossOrigin(origins = "*",maxAge = 3600)
public class ProductController {
    @Resource
    ProductMapper productMapper;

    @Resource
    ProductImageMapper productImageMapper;

    @LoginRequired
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Integer addProduct(@RequestBody AddProductDto addProductDto,HttpServletRequest request) {
        Integer id = Integer.parseInt(JwtUtil.getClaims(request.getHeader("token")).get("id").asString());
        Product product = new Product();
        BeanUtil.copyProperties(addProductDto,product);
        product.setBusinessId(id);
        product.setCreateTime(DateTime.now());
        int insert = productMapper.insert(product);
        if (insert == 1) {
            Product product1 = productMapper.selectOne(
                    new QueryWrapper<Product>()
                            .eq("business_id", id)
                            .eq("description", addProductDto.getDescription())
                            .eq("name", addProductDto.getName())
            );
            ProductImage productImage = new ProductImage();
            productImage.setPath(addProductDto.getPath());
            productImage.setProductId(product1.getId());
            productImageMapper.insert(productImage);
            return 1;
        }else {
            return 0;
        }
    }


    @LoginRequired
    @RequestMapping(value = "/getProductList",method = RequestMethod.GET)
    public List<ProductVo> getProductList(HttpServletRequest request) {
        Integer id = Integer.parseInt(JwtUtil.getClaims(request.getHeader("token")).get("id").asString());
        List<Product> business_id = productMapper.selectList(
                new QueryWrapper<Product>()
                        .eq("business_id", id)
        );

        List<ProductVo> productVos = new ArrayList<>();

        for (Product product : business_id) {
            ProductVo productVo = new ProductVo();
            BeanUtil.copyProperties(product,productVo);
            ProductImage product_id = productImageMapper.selectOne(
                    new QueryWrapper<ProductImage>()
                            .eq("product_id", product.getId())
            );

            productVo.setPath(product_id.getPath());
            productVo.setCreateTime(DateUtil.format(product.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
            productVos.add(productVo);
        }
        return productVos;
    }

    @RequestMapping(value = "/getAllProducts",method = RequestMethod.GET)
    public List<ProductVo> getAllProducts() {
        List<Product> business_id = productMapper.selectList(null);

        List<ProductVo> productVos = new ArrayList<>();

        for (Product product : business_id) {
            ProductVo productVo = new ProductVo();
            BeanUtil.copyProperties(product,productVo);
            ProductImage product_id = productImageMapper.selectOne(
                    new QueryWrapper<ProductImage>()
                            .eq("product_id", product.getId())
            );

            productVo.setPath(product_id.getPath());
            productVo.setCreateTime(DateUtil.format(product.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
            productVos.add(productVo);
        }
        return productVos;
    }


    @RequestMapping(value = "/getProductVoById",method = RequestMethod.GET)
    public ProductVo getProductVoById(Integer id) {

        Product product = productMapper.selectById(id);

        ProductVo productVo = new ProductVo();
        BeanUtil.copyProperties(product, productVo);
        ProductImage product_id = productImageMapper.selectOne(
                new QueryWrapper<ProductImage>()
                        .eq("product_id", product.getId())
        );

        productVo.setPath(product_id.getPath());
        productVo.setCreateTime(DateUtil.format(product.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
        return productVo;
    }
}
